home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / advanced97 / LINE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  4.1 KB  |  224 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <GL/glut.h>
  4.  
  5. #define CHECK_ERROR(str)                                           \
  6. {                                                                  \
  7.     GLenum error;                                                  \
  8.     if(error = glGetError())                                       \
  9.        printf("GL Error: %s (%s)\n", gluErrorString(error), str);  \
  10. }
  11.  
  12. int winWidth = 512;
  13. int winHeight = 512;
  14. GLboolean smooth = GL_FALSE;
  15. GLboolean dblbuf = GL_TRUE;
  16. GLfloat objangle[2] = {0.f, 0.f};
  17. GLfloat scale = 1.f;
  18. int active;
  19.  
  20. enum {X, Y, Z};
  21. enum {OBJ_ANGLE, OBJ_SCALE};
  22. enum {NOLIST, PLANE}; /* display lists */
  23.  
  24. /* load data structure from file */
  25. enum {VERTS, END};
  26.  
  27.  
  28. void
  29. reshape(int wid, int ht)
  30. {
  31.     winWidth = wid;
  32.     winHeight = ht;
  33.     glViewport(0, 0, wid, ht);
  34. }
  35.  
  36. void
  37. motion(int x, int y)
  38. {
  39.  
  40.     switch(active)
  41.     {
  42.     case OBJ_ANGLE:
  43.     objangle[X] = (x - winWidth/2) * 360./winWidth;
  44.     objangle[Y] = (y - winHeight/2) * 360./winHeight;
  45.     glutPostRedisplay();
  46.     break;
  47.     case OBJ_SCALE:
  48.     scale = x * 5./winWidth;
  49.     objangle[Y] = (y - winHeight/2) * 360./winHeight;
  50.     glutPostRedisplay();
  51.     break;
  52.     }
  53. }
  54.  
  55. void
  56. mouse(int button, int state, int x, int y)
  57. {
  58.     if(state == GLUT_DOWN)
  59.     switch(button)
  60.     {
  61.     case GLUT_LEFT_BUTTON: /* rotate the object */
  62.         active = OBJ_ANGLE;
  63.         motion(x, y);
  64.         break;
  65.     case GLUT_RIGHT_BUTTON: /* scale the object */
  66.         active = OBJ_SCALE;
  67.         motion(x, y);
  68.         break;
  69.     }
  70. }
  71.  
  72.  
  73. /* ARGSUSED1 */
  74. void key(unsigned char key, int x, int y)
  75. {
  76.     switch(key)
  77.     {
  78.     case 's': /* toggle line smoothing */
  79.     if(smooth)
  80.     {
  81.         glDisable(GL_LINE_SMOOTH);
  82.         glDisable(GL_BLEND);
  83.         smooth = GL_FALSE;
  84.         printf("Turn off OpenGL line smoothing\n");
  85.     }
  86.     else
  87.     {
  88.         glEnable(GL_LINE_SMOOTH);
  89.         glEnable(GL_BLEND);
  90.         printf("Turn on OpenGL line smoothing\n");
  91.         smooth = GL_TRUE;
  92.     }
  93.     glutPostRedisplay();
  94.     break;
  95.     case '\033':
  96.     exit(0);
  97.     break;
  98.    case '?':
  99.    case 'h':
  100.    case 'H':
  101.     default:
  102.     fprintf(stderr, "Keyboard commands:\n\n"
  103.         "s - toggle smooth line mode\n");
  104.     break;
  105.     }
  106.  
  107. }
  108.  
  109.  
  110. void
  111. loader(char *fname)
  112. {
  113.     FILE *fp;
  114.     GLfloat x, y, z;
  115.     int state = END;
  116.     int read;
  117.  
  118.     fp = fopen(fname, "r");
  119.     if (!fp) {
  120.         printf("can't open file %s\n", fname);
  121.     exit(1);
  122.     }
  123.  
  124.     glNewList(PLANE, GL_COMPILE);
  125.     while(!feof(fp))
  126.     {
  127.     switch(state)
  128.     {
  129.     case END:
  130.         read = fscanf(fp, " v");
  131.         if(read < 0) /* hit eof */
  132.         break;
  133.         state = VERTS;
  134.         glBegin(GL_LINE_STRIP);
  135.         break;
  136.     case VERTS:
  137.         read = fscanf(fp, " %f %f %f", &x, &y, &z);
  138.         if(read == 3)
  139.         glVertex3f(x, y, z);
  140.         else
  141.         {
  142.         fscanf(fp, " e");
  143.         glEnd();
  144.         state = END;
  145.         }
  146.         break;
  147.     }
  148.     }
  149.     glEndList();
  150.     fclose(fp);
  151. }
  152.  
  153. void redraw(void)
  154. {
  155.     glClear(GL_COLOR_BUFFER_BIT);
  156.  
  157.     glPushMatrix();
  158.     glRotatef(objangle[X], 0.f, 1.f, 0.f); /* rotate object */
  159.     glRotatef(objangle[Y], 1.f, 0.f, 0.f);
  160.     glScalef(scale, scale, scale);
  161.  
  162.     glCallList(PLANE);
  163.  
  164.     glPopMatrix();
  165.  
  166.     if(dblbuf)
  167.     glutSwapBuffers(); 
  168.     else
  169.     glFlush(); 
  170.  
  171.     CHECK_ERROR("OpenGL Error in redraw()");
  172. }
  173.  
  174. void main(int argc, char **argv)
  175. {
  176.     glutInit(&argc, argv);
  177.     glutInitWindowSize(winWidth, winHeight);
  178.     if(argc > 1)
  179.     {
  180.     char *args = argv[1];
  181.     GLboolean done = GL_FALSE;
  182.     while(!done)
  183.     {
  184.         switch(*args)
  185.         {
  186.         case 's': /* single buffer */
  187.         printf("Single Buffered\n");
  188.         dblbuf = GL_FALSE;
  189.         break;
  190.         case '-': /* do nothing */
  191.         break;
  192.         case 0:
  193.         done = GL_TRUE;
  194.         break;
  195.         }
  196.         args++;
  197.     }
  198.     }
  199.     if(dblbuf)
  200.     glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
  201.     else
  202.     glutInitDisplayMode(GLUT_RGBA);
  203.  
  204.     (void)glutCreateWindow("load and draw a wireframe image");
  205.     glutDisplayFunc(redraw);
  206.     glutReshapeFunc(reshape);
  207.     glutKeyboardFunc(key);
  208.     glutMotionFunc(motion);
  209.     glutMouseFunc(mouse);
  210.  
  211.     glMatrixMode(GL_PROJECTION);
  212.     glOrtho(-1., 1., -1., 1., -5., 5.);
  213.     glMatrixMode(GL_MODELVIEW);
  214.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  215.     glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
  216.  
  217.     loader("data/f15.data");
  218.  
  219.     CHECK_ERROR("OpenGL Error in main()");
  220.  
  221.     key('?', 0, 0); /* print usage message */
  222.     glutMainLoop();
  223. }
  224.